/*=============================================================================

  MANAGER IDLE STATE MODULE

  FILE: SYSmgr_S_MANUAL_READ.cpp

  DESCRIPTION

    Manager Idle State Implementation

  EXTERNALIZED FUNCTIONS


  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

  EDIT HISTORY

  ---------  -------  -------------------------------------------------------
  when       who     what, where, why
  ---------  -------  -------------------------------------------------------
  06/28/03   nso4th   Version 0.1

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include "SYSmgr.h"

/*=============================================================================
CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
LOCALIZED VARIABLE DEFINITION
=============================================================================*/
static dword	gStep = STATE_INIT_STEP;
#define SETSTEP(s)		((gStep) = (s))
#define GETSTEP()		(gStep)

typedef enum
{
	RESET_INIT_STEP = STATE_INIT_STEP,
	
	RESET_HOME_STEP,
	RESET_NEXT_STEP,

	RESET_MOVEREQ_STEP,
	RESET_MOVEDONE_STEP,
	RESET_DONE_STEP,

	RESET_ERR_STEP,
	RESET_EXIT_STEP
} RESET_RESET_Step_e;

/*=============================================================================
EVENT PROCESSING
=============================================================================*/
dword SYSmgr::RESET_ProcRESET(dword dwType, dword dwEvent, dword wParam, dword lParam)
{
	dword				evtID, event;
	static boolean		sysErr = FALSE;
	boolean				reqStop = FALSE;
	
	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);

	// [Internal Command Response]
	if(IS_CMD_EVENT(dwType))
	{
		switch(dwEvent)
		{
		case STATE_STOP_C:
		{
			SETSTEP(RESET_EXIT_STEP);
			reqStop = TRUE;
			break;
		}
		}
	}
	/*else if(dwType == SYS_MGR)
	{
		return	FALSE;
	}*/

	// [Sequence Control]
	switch(GETSTEP())
	{
	case RESET_INIT_STEP:
	{
		sysErr = FALSE;
		if(m_pMot->IsHomeDone() == FALSE)
		{
			LOGINFO("Homing req.");
			
			if(State_MinPrevious() != RESET_HOME_S)
			{
				m_homeReq = MOT_AXIS_ALL;
				SETSTEP(RESET_HOME_STEP);
			}
			else  // When homing is not completed !
			{
				ERR_REPORT("ERROR", "Homing is the failure to perform can not be reset.");
				sysErr = TRUE;
				SETSTEP(RESET_EXIT_STEP);
			}
			return	TRUE;
		}
		LOGINFO("=====================");
		LOGINFO(" System reset start");
		LOGINFO("=====================");

		/*LOGINFO("[RESET] START");*/
		WATCH_reset();
		SETSTEP(RESET_NEXT_STEP);
		break;
	}
	case RESET_HOME_STEP:
	{
		State_Minor(RESET_HOME_S);
		SETSTEP(STATE_INIT_STEP);
		break;
	}
	case RESET_NEXT_STEP:
	{
		SETSTEP(RESET_MOVEREQ_STEP);
		break;
	}
	//---------------------------------
	// MOTION
	//---------------------------------
	case RESET_MOVEREQ_STEP:
	{
		LOGINFO("Move the initial position !");
		// Go to loading position !!
		LOGINFO("[RESET] Move the initial position (%.1f, %.1f, %.1f)", HMot_TblPosition(HMOT_X_LOAD_POS),\
		HMot_TblPosition(HMOT_Y_LOAD_POS), HMot_TblPosition(HMOT_Z_LOAD_POS));
		
		HMot_Move(MOT_AXIS_X, HMot_TblPosition(HMOT_X_LOAD_POS), HMot_TblVelocity(HMOT_X_LOAD_VEL));
		HMot_Move(MOT_AXIS_Y, HMot_TblPosition(HMOT_Y_LOAD_POS), HMot_TblVelocity(HMOT_Y_LOAD_VEL));
		SETSTEP(RESET_MOVEDONE_STEP);
	
		break;
	}
	case RESET_MOVEDONE_STEP:
	{
		if((HMot_IsMoveDone(MOT_AXIS_X, HMot_TblPosition(HMOT_X_LOAD_POS))) \
			&& (HMot_IsMoveDone(MOT_AXIS_Y, HMot_TblPosition(HMOT_Y_LOAD_POS))))
		{
			if((HMot_IsExactPosition(MOT_AXIS_X, HMot_TblPosition(HMOT_X_LOAD_POS))) \
				&& (HMot_IsExactPosition(MOT_AXIS_Y, HMot_TblPosition(HMOT_Y_LOAD_POS))))
			{
				LOGINFO("[RESET]  Complete the move");
				SETSTEP(RESET_DONE_STEP);
			}
			else
			{
				ERR_REPORT("[RESET]","Failure to move in standby position");
				SETSTEP(RESET_ERR_STEP);
			}
		}
		break;
	}
	//---------------------------------
	// DONE !
	//---------------------------------
	case RESET_DONE_STEP:
	{
		LOGINFO("System reset completed !");
		SETSTEP(RESET_EXIT_STEP);
		break;
	}

	case RESET_ERR_STEP:
	{
		sysErr = TRUE;
		SETSTEP(RESET_EXIT_STEP);
		break;
	}

	case RESET_EXIT_STEP:
	default:
	{
		SETSTEP(STATE_INIT_STEP);
		
		if(sysErr)
		{
			sysErr = FALSE;
			State_Change(IDL_S, IDL_ERR_S);
		}
		else
		{
			State_Change(IDL_S, IDL_IDL_S);
		}
		break;
	}
	}

	return	FALSE;
}

/*--- END OF SYSmgr_S_RESET_RESET.CPP ---*/
